define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    $.widget('rk.crmMenuDialogCtrl', {
            _create: function(){
                var me = this;
                var elem = me.element; 
            },
            _init: function(){
                var me = this;
                var elem = me.element;

                me._getCrmSourceData(); 
            },
            crmSourceData:null,
            _getCrmSourceData: function(){
                var me = this;
                var elem = me.element;
                elem.append('<div class="for_loading" style="width:100%;height:600px;"></div>');
                elem.find('div.for_loading').beLoading({size:'large',delay:0});
                rk.repositionDialog();
                $.postJson("/json/global_menu/crm.action").done(function(json){
                    if(json.status===0){
                        me.crmSourceData = json.data;
                        elem.find('div.for_loading').remove();
                        me._initData();
                    }else{
                        me.globalNotice.pageNoticeBtnCtrl('setNum',0);
                    }
                });
            },
            _initData: function(){
                var me = this;
                var elem = me.element;
                var menuList = elem.find('.menu-list');
                var sourceList = elem.find('.source-list');


                var tpl = require('../../../tmpl/frame/crm_menucfg_dialog.tpl');
                var tplHtml = rk.templateText(tpl,me.crmSourceData);
                elem.html(tplHtml);
                
                elem.find('.menu-list').sortable();

                me.hideAllSourceItem();
                elem.find('.accordion-heading:first').next().show();
                elem.find('.accordion-heading:first').addClass('accordion-heading-active');
                $(elem).on('click', '.accordion-heading', function(e){
                    var sourceList = elem.find('.source-list');
                    sourceList.css('height','auto');

                    var btn = $(this);
                    if(!btn.next().is(':visible')){
                        me.hideAllSourceItem();
                        btn.next().slideDown();
                        btn.addClass('accordion-heading-active');
                    }
                });

                elem.find('.accordion-ck').each(function(){
                    var ck = $(this);
                    ck.button();

                    if(ck[0].checked){
                        var item = {};
                        item.id = ck.attr('id').slice(8);
                        item.link = ck.val();
                        item.name = ck.next().text();
                        item.icon = ck.attr('icon-data');
                        me.appendItem(item,ck.parent().parent().parent().attr('s_name'),0);  
                    }

                    me.refreshBadge(ck.attr('id'));

                    ck.click(function(){
                        if(ck[0].checked){
                            var additem = {};
                            additem.name = ck.next().find('span').html();
                            additem.link = ck.val();
                            additem.id = ck.attr('id').slice(8);
                            additem.icon = ck.attr('icon-data');

                            var sname = ck.parent().parent().parent().attr('s_name');

                            var n_ck = ck.parent().parent().nextAll().find('.accordion-ck:checked');
                            var nId = 0;
                            if(n_ck.length > 0){
                                nId = n_ck.attr('id').slice(8);
                            }
                            me.appendItem(additem,sname,nId);
                            me.refreshBadge(ck.attr('id'));
                        }else{
                            me.removeItem(ck.attr('id').slice(8));
                        }
                    });
                });

                rk.repositionDialog();

            },
            //Single selected item input function
            appendItem: function(item,containerName,nId){
                var me = this;
                var elem = me.element;
                var menuList = elem.find('.menu-list');
                if(menuList.find('li[crm_menu_id="'+item.id+'"]').length === 0){
                    if(item.link === 'cas.action'){
                        item.link = 'case.action';
                    }
                    var newItem = '<li sd_name="'+containerName+'" class="menu-list-item" crm_menu_id="'+item.id+'" crm_menu_link="'+item.link+'" crm_menu_icon="'+item.icon+'"><span>'+item.name+'</span><span class="tool"><a href="javascript:;" class="close"></a></span></li>';
                    if(nId === 0){
                        if(containerName !== '0' && menuList.find('li[sd_name="'+containerName+'"]').length === 0){
                            var newName =  containerName - 1;
                            while(menuList.find('li[sd_name="'+newName+'"]').length === 0 && newName > 0){
                                newName = newName - 1;
                            }
                            if(menuList.find('li[sd_name="'+newName+'"]').length > 0){
                                menuList.find('li[sd_name="'+newName+'"]:last').after($(newItem));
                            }else{
                                menuList.append($(newItem));
                            }
                        }
                        else if(menuList.find('li[sd_name="'+containerName+'"]').length !== 0)
                        {
                            menuList.find('li[sd_name="'+containerName+'"]:last').after($(newItem));
                        }
                        else
                        {
                            if(containerName === '0'){
                                menuList.prepend($(newItem));
                            }else{
                                menuList.append($(newItem));
                            }
                        }
                    }else{
                        menuList.find('li[crm_menu_id="'+nId+'"]').before($(newItem));
                    }
                    menuList.find('li[crm_menu_id="'+item.id+'"]').find('a').click(function(){
                        me.removeItem(item.id);
                    });
                }

                var sourceList = elem.find('.source-list');
                if(sourceList.height() < menuList.height()){
                    sourceList.css('height',menuList.height());
                }
            },
            removeItem: function(id){
                var me = this;
                var elem = me.element;
                var menuList = elem.find('.menu-list');
                
                menuList.find('li[crm_menu_id='+id+']').remove();
                elem.find('#menu_ck_'+id).next().trigger('click');
                me.refreshBadge('menu_ck_'+id);

                var sourceList = elem.find('.source-list');
                if(sourceList.height() > menuList.height()){
                    var conH = 0;
                    sourceList.find('div').each(function(){
                        var t_div = $(this);
                        if(t_div.is(':visible')){
                            conH += parseInt($(this).height());
                        }
                    });
                    if(conH < menuList.height()){
                        sourceList.css('height',menuList.height());
                    }
                }
            },
            hideAllSourceItem: function(){
                $('.accordion-body').each(function(e){
                    var content = $(this);
                    content.slideUp();
                    if(content.prev().hasClass('accordion-heading-active')){
                        content.prev().removeClass('accordion-heading-active');
                    }
                });
            },
            refreshBadge: function(ck_id){
                var me = this;
                var elem = me.element;
                var ck = elem.find('#'+ck_id);
                var select_ck_num = ck.parent().parent().parent().find('.accordion-ck:checked').length;
                var total_ck_num = ck.parent().parent().parent().find('.accordion-ck').length;
                ck.parent().parent().parent().parent().prev().find('.badge').html("(" + select_ck_num + "/" + total_ck_num+ ")");
            },
            getData: function(){
                var me = this;
                var elem = me.element;
                var menuList = elem.find('.menu-list');
                var selected_menu = [];
                menuList.find('.menu-list-item').each(function(){
                    var eachItem = $(this);
                    var temp_item = {};
                    temp_item.id = eachItem.attr('crm_menu_id');
                    temp_item.name = eachItem.find('span:first').html();
                    temp_item.link = eachItem.attr('crm_menu_link');
                    temp_item.icon = eachItem.attr('crm_menu_icon');
                    selected_menu.push(temp_item);
                });
                return selected_menu;
            }
    });
});